package net.zjitc.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebParam.Mode;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.zjitc.Utils.CommonUtils;
import net.zjitc.pojo.CartItem;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Evaluate;
import net.zjitc.pojo.Order;
import net.zjitc.pojo.Orderitem;
import net.zjitc.pojo.Product;
import net.zjitc.pojo.User;
import net.zjitc.service.CartService;
import net.zjitc.service.CategoryService;
import net.zjitc.service.OrderService;
import net.zjitc.service.ProductService;
import org.ietf.jgss.Oid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.RedirectView;
import sun.awt.geom.AreaOp.EOWindOp;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/5/17 14:24
 * @description:
 */
//订单相关
@Controller
public class AdminOrder {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ProductService productService;
  //所有订单(后台)
  @RequestMapping("/adminOrder")
  public ModelAndView adminOrder(){
    ModelAndView modelAndView =new ModelAndView();
    List<Order> list=orderService.findAll();
    modelAndView.addObject("olist",list);
    modelAndView.setView(new InternalResourceView("shelll68/order/column.jsp"));
    return modelAndView;
  }
  //提交订单（前台）
  @RequestMapping("/createOrder")
  public ModelAndView createOrder(HttpSession session){
    ModelAndView mv = new ModelAndView();
    User user= (User)session.getAttribute("session_user");
    if (user==null){
      mv.setView(new InternalResourceView("/loginUI"));
    }
    Order order=new Order();
    order.setOid(CommonUtils.uuid());
    order.setAddr(user.getAddr());
    order.setPhone(user.getPhone());
    order.setName(user.getName());
    order.setState(1);
    order.setUser(user);
    order.setUid(user.getUid());
    order.setOrdertime(CommonUtils.dateToString(new Date()));

    List<CartItem> cartItemList=cartService.findByUid(user.getUid());
    if (cartItemList== null){
      mv.setView(new RedirectView("user/order.jsp"));
    }

    List<Orderitem> orderitemList=new ArrayList<Orderitem>();
    double total=0.0;
    for (CartItem item:cartItemList){
      Orderitem orderitem=new Orderitem();
      orderitem.setOrder(order);
      orderitem.setProduct(item.getProduct());
      orderitem.setOid(order.getOid());
      orderitem.setPid(item.getProduct().getPid());
      orderitem.setOsatate(0);
      orderitem.setCount(item.getQuantity());
      orderitem.setSubtotal(item.getProduct().getShop_price()*item.getQuantity());
      total=total+item.getProduct().getShop_price()*item.getQuantity();
      orderitemList.add(orderitem);
    }
    order.setOrderitemList(orderitemList);
    order.setTotal(total);
    System.out.println(orderitemList);
    int count =orderService.add(order,orderitemList);
    mv.addObject("order",order);
    List<Category> clist=categoryService.findAll();
    mv.addObject("clist",clist);
    mv.setView(new InternalResourceView("/user/order.jsp"));
    return mv;
  }
  //我的订单（前台）
  @RequestMapping("/myOrder")
  public ModelAndView shopIndex(HttpSession session) {
    ModelAndView mv = new ModelAndView();
    User user = (User) session.getAttribute("session_user");
    if (user == null) {
      mv.setView(new InternalResourceView("/loginUI.do"));
    }
    //通过用户找到订单
    List<Order> orders = orderService.findByUid(user.getUid());
    mv.addObject("orders", orders);
    //菜单栏显示一级菜单
    List<Category> clist = categoryService.findAll();
    mv.addObject("clist", clist);
    mv.setView(new InternalResourceView("/user/orderList.jsp"));
    mv.addObject("trade_status");
    System.out.println("trade_status");
    return mv;
  }
  //未付款
  @RequestMapping("/order_payOrder")
  public ModelAndView order_payOrder(@RequestParam(name = "oid")String oid,HttpSession session){
    ModelAndView mv = new ModelAndView();
    System.out.println(session);
    List<Orderitem> abc = orderService.findOrderitemByOid(oid);
    String WIDout_trade_no =oid;
    double total=0.0;
    for (Orderitem o:abc){
      total=total+o.getSubtotal();
    }
    String WIDtotal_amount =String.valueOf(total);
    mv.addObject("WIDout_trade_no",WIDout_trade_no);
    mv.addObject("WIDtotal_amount",WIDtotal_amount);
    mv.setViewName("zhifubao/alipay.trade.page.pay");
    //mv.setView(new InternalResourceView("/zhifubao/alipay.trade.page.pay.jsp"));
    return mv;
  }
  //已付款
  @RequestMapping("/order_editState")
  public ModelAndView order_editState(
      @RequestParam(name = "out_trade_no")String oid
  ){
    ModelAndView mv= new ModelAndView();
    Order order =new Order();
    order.setState(2);
    order.setOid(oid);
    Integer integer =orderService.editPayState(order);
    mv.setView(new InternalResourceView("myOrder.do"));
    return mv;
  }
  //发货
  @RequestMapping("/adminOrder_editState")
  public ModelAndView adminOrder_editState(
      @RequestParam(name = "oid")String oid
  ){
    ModelAndView modelAndView= new ModelAndView();
    Order order =new Order();
    order.setState(3);
    order.setOid(oid);
    Integer integer = orderService.editPayState(order);
    modelAndView.setView(new InternalResourceView("/adminOrder.do"));
    return modelAndView;
  }
  //收货
  @RequestMapping("/order_editOrder")
  public ModelAndView order_editOrder(
      @RequestParam(name = "oid")String oid
  ){
      ModelAndView modelAndView =new ModelAndView();
      Order order =new Order();
      order.setState(4);
      order.setOid(oid);
      Integer integer1 =orderService.editPayState(order);
      Orderitem orderitem =new Orderitem();
      orderitem.setOsatate(1);
      orderitem.setOid(oid);
      Integer integer2 =orderService.editOsatate(orderitem);
      modelAndView.setView(new InternalResourceView("myOrder.do"));
      return modelAndView;
  }
  //评价
  @RequestMapping("/evaluate_findByPid")
  public ModelAndView evaluate_findByPid(
      @RequestParam(name = "pid")Integer pid,
      @RequestParam(name = "itemid")Integer itemid
  ){
        ModelAndView mv= new ModelAndView();
    Product product=productService.findByPid(pid);
    mv.addObject("product",product);
    //获取订单项id
    mv.addObject("itemid",itemid);
    //菜单栏显示一级菜单
    List<Category> clist=categoryService.findAll();
    mv.addObject("clist",clist);
    mv.setView(new InternalResourceView("user/addEvaluate.jsp"));
    return mv;
  }
  //添加评价
  @RequestMapping("/evaluate_saveEval")
  public ModelAndView evaluate_saveEval(
      @RequestParam(name = "pid")Integer pid,
      @RequestParam(name = "title")String title,
      @RequestParam(name = "content")String content,
      @RequestParam(name = "itemid")Integer itemid,
      HttpSession session){
        ModelAndView mv = new ModelAndView();
    //获取产品
    Product product=productService.findByPid(pid);
    //获取session user
    User user= (User) session.getAttribute("session_user");
    //设置评价
    Evaluate evaluate=new Evaluate();
    evaluate.setPid(product.getPid());
    evaluate.setUid(user.getUid());
    evaluate.setContent(content);
    evaluate.setTitle(title);
    evaluate.setDate(new Date());
    Integer count=productService.addEvaluate(evaluate);
    Integer countItemid=orderService.editOstateByItemid(itemid);
    mv.setView(new RedirectView("/productFindByPid.do?pid="+pid));
    return mv;
  }
}
